<?php

namespace frontend\controllers;

use Yii;
use frontend\components\Controller;
use common\models\user\UserHistory;

/**
 * 课程详情接口
 * SchoolController
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class CourseController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['GET'],
        ];
    }

    public $modelClass = 'frontend\models\CourseSearch';

    /**
     * {@inheritdoc}
     */
    public function actions() {
        return [
            'view' => [
                'class' => 'yii\rest\ViewAction',
                'modelClass' => $this->modelClass,
            ],
        ];
    }

    public function afterAction($action, $result) {
        if ($action->id == 'view' && $result->id && $this->token->user_id) {
            $model = UserHistory::findOrNew([
                'user_id' => $this->token->user_id,
                'course_id' => $result->id,
            ]);
            if (!$model->isNewRecord) {
                $model->updated_at = time();
            }
            $model->save(false);
        }
        return parent::afterAction($action, $result);
    }

}
